if(autocompleter == undefined){
	var autocompleter = {};
}
autocompleter.data = {};
autocompleter.selectedTR = null;
autocompleter.divID = "_autocompleterFloatDiv";
autocompleter.tableID = "_autocompleterFloatTable";
autocompleter.pageLayer = new Best.PageLayer();
autocompleter.showMenuTimer = 0;
autocompleter.pageLayerStopEvent = false;

/**
 * 是否用查询的数据填充过
 */
autocompleter.filed = true;
/**
 * 填充过的表单元素
 */
autocompleter.filledFormElements = [];
autocompleter.inputElementLastValue = "";
autocompleter.onkeydown = function(event){
	var element = Event.element(event);
	autocompleter.inputElement = element;
	switch(event.keyCode){
		case 13:
			autocompleter.onenter(event);
			return true;
		case 27:
			autocompleter.oncancel();
			return true;
		case 33:
			autocompleter.onpageup();
			return true;
		case 34:
			autocompleter.onpagedown();
			return true;						
		case 38:
			autocompleter.onchangeSelectedTR(1);
			return true;
		case 40:
			autocompleter.onchangeSelectedTR(2);
			return true;	
	}
	return true;
}
autocompleter.onfocus = function(event){
	var element = Event.element(event);
}
autocompleter.onchange = function(event){
	if(event && event.propertyName != 'value'){
		return;
	}
	if(autocompleter.filled){
		autocompleter.filled = false;
		return;
	}
	var element = autocompleter.inputElement;
    
    if (autocompleter.inputElementLastValue != element.value) {
        if ("" == Utils.LTrim(Utils.RTrim(autocompleter.inputElement.value))) {
            autocompleter.inputElementLastValue = element.value;
			return false;
        }
		autocompleter.inputElementLastValue = element.value;
    }
	else{
		return false;
	}
	if(autocompleter.showMenuTimer != null){
		window.clearTimeout(autocompleter.showMenuTimer);
	}
	autocompleter.showMenuTimer = setTimeout(autocompleter.showMenu, 500);
	
	autocompleter.filled = false;
	return true;		
}
autocompleter.showMenu = function(){
	var element = autocompleter.inputElement;
	if(autocompleter.list){
		var keyword = Utils.LTrim(Utils.RTrim(element.value));
		var t = autocompleter.localSearch(keyword, autocompleter.list);
		autocompleter.showListDiv(element, t);
	}else{
		var url = autocompleter.listUrl;
		$j.ajax({
			 type:"POST", 
			 async:true,
			 data:"keyword="+element.value,
			 url:url,
			 success:autocompleter.getListDataCB.bind(this, element)
		});
	}
}
autocompleter.onblur = function(){
	if(!autocompleter.filled){
		var index = autocompleter.getSelectedTRIndex();
		Logger.debug("onblur: index = " + index);
		if(index == -1){
			autocompleter.clearFilledFormElements();
		}else{
			if(index == 0){
				var row = autocompleter.data[index];
				if(row == undefined){
					autocompleter.clearFilledFormElements();
				}
			}
			autocompleter.fillSelectedData();
		}
	}
}
/**
 * 清空已填充的表单元素
 */
autocompleter.clearFilledFormElements = function(){
	autocompleter.inputElement.value = '';
	for(var i=0; i<autocompleter.filledFormElements.length; i++){
		autocompleter.filledFormElements[i].value = '';
	}
}
autocompleter.onpageup = function(){
	var div = $(autocompleter.divID);
	Utils.doScroll(div, "scrollbarPageUp");
	autocompleter.changeSelectedTROnScroll(1);
}
autocompleter.onpagedown = function(){
	var div = $(autocompleter.divID);
	Utils.doScroll(div, "scrollbarPageDown");
	autocompleter.changeSelectedTROnScroll(2);
}
/**
 * @param direction
 * 			1: 向上翻页
 * 			2: 向下翻页
 */
autocompleter.changeSelectedTROnScroll = function(direction){
	var table = $(autocompleter.tableID);
	if(table){
		var rows = table.rows;
		var pageTRNum = Math.ceil($(autocompleter.divID).clientHeight/rows[0].offsetHeight);
		var nextSelectedTR = autocompleter.selectedTR;
		for(var i=0; i<pageTRNum; i++){
			if(direction == 1){
				if(nextSelectedTR.previousSibling != null){
					nextSelectedTR = nextSelectedTR.previousSibling;
				}else{
					break;
				}
			}else if(direction == 2){
				if(nextSelectedTR.nextSibling != null){
					nextSelectedTR = nextSelectedTR.nextSibling;
				}else{
					break;
				}
				
			}
		}
		autocompleter.onmouseover(nextSelectedTR);
	} 
}
autocompleter.localSearch = function(keyword, list){
	var result = [];
	for(var i=0; i<list.length; i++){
		var t = list[i];
		if(t.indexOf(keyword) != -1){
			result.push(t);
		}
	}
	if(result.length == 0){
		result = list;
	}
	return result;
}
autocompleter.onenter = function(event){
	var element = autocompleter.inputElement;
	var div = $(autocompleter.divID);
	if(div && div.style.display != "none"){
		autocompleter.fillSelectedData();
		autocompleter.oncancel();
	}
	Event.stop(event);
}
autocompleter.getListDataCB = function(element, dataFromServer){
	dataFromServer = eval("(" + dataFromServer + ")");
	var data;
	if(dataFromServer.length == 5) {
		data = eval("dataFromServer[4]." + autocompleter.listKey);
	}
	else
		data = eval("dataFromServer." + autocompleter.listKey);
	autocompleter.showListDiv(element, data);
}
autocompleter.showListDiv = function(element, data){
	var keyword = Utils.LTrim(Utils.RTrim(autocompleter.inputElement.value));
	autocompleter.data = data;
	/*
	if(data.length == 1){
		autocompleter.fillData(0);
		return;
	}
	*/
	var pos = Position.cumulativeOffset(element);
	autocompleter.pageLayer.addLayer(
				{
					layerID: autocompleter.divID,
					stopEvent: autocompleter.pageLayerStopEvent,
					closeFunc: autocompleter.oncancel
				});
	var div = document.createElement("div");
	div.id = autocompleter.divID;
	div.className = "autocompleterDiv";
	div.style.left = pos[0] + "px";
	div.style.top = (pos[1] + element.offsetHeight) + "px";
	div.style.padding = "2px";
	div.style.color = "#0055AA";
	div.style.border = "1px solid #7F9DB9";
	if(autocompleter.listWidth != -1){
		div.style.width = autocompleter.listWidth + "px";
	}else{
		div.style.width = (element.offsetWidth + 16) + "px";
	}
	if(autocompleter.listHeight != -1){
		div.style.height = autocompleter.listHeight + "px";
	}
	
	var table = '<table id="' + autocompleter.tableID + '" cellspacing="0" style="width:100%">';
	if(data.length == 0){
		table += "<tr><td>" + autocompleter.messageNoData + "</td></tr>";
	}else{
		for(var i=0; i<data.length; i++){
			var item = data[i];
			table += '<tr onclick="autocompleter.onclick();" onmouseover="autocompleter.onmouseover(this);" onmouseout="autocompleter.onmouseout(this);">';
			if(autocompleter.listColumns.length != 0){
				for(var j=0; j<autocompleter.listColumns.length; j++){
					var p = autocompleter.listColumns[j];
					var value;
					if(p.indexOf('.') == -1){
						value = item[p];
					}else{
						value = eval('item.' + p);
					}
					table += "<td>" + Utils.highlightKeyword(keyword, value) + "</td>";
				}
			}else{
				if(typeof item == "object"){
					for(p in item){
						if( typeof item[p] == "string"){
							table += "<td>" + Utils.highlightKeyword(keyword, item[p]) + "</td>";
						}
					}
				}else{
					table += "<td>" + Utils.highlightKeyword(keyword, item) + "</td>";
				}
			}
			table += "</tr>";
		}
	}
	div.innerHTML = table;

	document.body.appendChild(div);
	if(Browser.isIE()){
		$(autocompleter.tableID).style.width = "88%";
	}
	if($(autocompleter.tableID)){					
		var rows = $(autocompleter.tableID).rows;			
		if(rows.length > 0){
			autocompleter.selectedTR = rows[0];
			autocompleter.onmouseover(autocompleter.selectedTR);
		}
	}
	Utils.hideShowCovered(div);
}

/**
 * @param direction
 * 			1: 向上移
 * 			2: 向下移
 */
autocompleter.onchangeSelectedTR = function(direction){
	var selectedTR = autocompleter.selectedTR;
	if($(autocompleter.divID) && selectedTR){
		if(direction == 1){
			if(selectedTR.previousSibling != null){
				var div = $(autocompleter.divID);
				if(selectedTR.previousSibling.offsetTop - div.scrollTop < 0)
					selectedTR.previousSibling.scrollIntoView(true);
				autocompleter.onmouseout(selectedTR);
				autocompleter.onmouseover(selectedTR.previousSibling);
			}
		}else if(direction == 2){
			if(selectedTR.nextSibling != null){
				var div = $(autocompleter.divID);
				if(selectedTR.nextSibling.offsetTop - div.scrollTop > div.offsetHeight-10)
					selectedTR.nextSibling.scrollIntoView(false);
				autocompleter.onmouseout(selectedTR);
				autocompleter.onmouseover(selectedTR.nextSibling);
			}
		}
	}
}
autocompleter.onmouseover = function(element){
	autocompleter.onmouseout(autocompleter.selectedTR);
	MouseEvent.onmouseover(element);
	autocompleter.selectedTR = element;
}
autocompleter.onmouseout = function(element){
	MouseEvent.onmouseout(element);
}
autocompleter.oncancel = function(){
	var div = $(autocompleter.divID);
	if(div != null){
		div.parentNode.removeChild(div);
	}
	Utils.hideShowCovered(div);
}
autocompleter.onclick = function(){
	autocompleter.fillSelectedData();
	autocompleter.oncancel();
}
autocompleter.fillSelectedData = function(){
	var index = autocompleter.getSelectedTRIndex();
	if(!autocompleter.filled){
		autocompleter.fillData(index);
	} 
}
autocompleter.fillData = function(index){
	if(index != -1){
		var row = autocompleter.data[index];
		var form = autocompleter.inputElement.form;
		
		var groupColumnValue = [];
		var value;
		if(row != undefined){
			if(typeof row == "object"){
				if(autocompleter.autoFillColumns.length > 0){
					for(var i=0; i<autocompleter.autoFillColumns.length; i++){
						var t, p;
						var elm,val;
						//autoFillColumns修改为map表示形式{'formelement,jsonvalue'}
						p = autocompleter.autoFillColumns[i].split(',');
						//页面上需要填充的元素
						elm = p[0];
						//json中的填充对象名
						val = p[1];
						if(val.indexOf('.') == -1){
							value = row[val];
						}else{
							value = row[val];
							if(value == undefined){
								value = eval('row.' + val);
							}
						}
						if(form == null){
							t = $(elm);
						}else{
							t = form[elm];
						}
						if(t != null){
							
							t.value = value;
							if(autocompleter.filledFormElements.indexOf(t) == -1){
								autocompleter.filledFormElements.push(t);
							}
						}
						
					}
					for(p in row){
						var t;
						if(p.indexOf('.') == -1){
							value = row[p];
						}else{
							value = row[p];
							if(value == undefined){
								value = eval('row.' + p);
							}
						}
						
						var index = autocompleter.groupingColumns.indexOf(p);
						if(index != -1){
							groupColumnValue[index] = value; 
						}
					}
				}else{
					for(p in row){
						var t;
						if(p.indexOf('.') == -1){
							value = row[p];
						}else{
							value = eval('row.' + p);
						}
						if(form == null){
							t = $(p);
						}else{
							t = form[p];
						}
						if(t != null){
							if(t == autocompleter.inputElement){
								autocompleter.inputElementLastValue = value;
							}
							t.value = value;
							if(autocompleter.filledFormElements.indexOf(t) == -1){
								autocompleter.filledFormElements.push(t);
							}
						}
						var index = autocompleter.groupingColumns.indexOf(p);
						if(index != -1){
							groupColumnValue[index] = value; 
						}
					}
				}
			}else{
				
				if(autocompleter.filledFormElements.indexOf(t) == -1){
					autocompleter.filledFormElements.push(autocompleter.inputElement);
				}
				autocompleter.inputElementLastValue = row;
				autocompleter.inputElement.value = row;
			}
			
			if(autocompleter.groupingColumns.length > 0){
				autocompleter.inputElementLastValue = groupColumnValue.join("/");
				autocompleter.inputElement.value = groupColumnValue.join("/");
				
			}
			autocompleter.filled = true;
			if(autocompleter.onCommit != null){
				autocompleter.onCommit(row);
			}
		}
	}
}

autocompleter.getSelectedTRIndex = function(){
	if($(autocompleter.tableID) != undefined){
		var rows = $(autocompleter.tableID).rows;
		for(var i=0; i<rows.length; i++){
			if(rows[i] == autocompleter.selectedTR){
				return i;
			}
		}
	}
	return -1;
}
autocompleter.onArrowClick = function(){
	autocompleter.pageLayerStopEvent = true;
	autocompleter.showMenu();
}